AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

local maxentititable = 5
function ENT:Initialize()
	self.Entity:SetModel("models/props_c17/substation_stripebox01a.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.done = false
	local phys = self.Entity:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake()  	
	end  
end

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "cargo_upgrade" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:OnRemove()
	if self.done then
		if self.cargo:IsValid() then
			self.cargo.maxentititable = self.cargo.maxentititable -5
			for index,ent in pairs(self.cargo.class) do 
				if (index > self.cargo.maxentititable) then
					ent = nil
				end
			end
		end
	end
end

function ENT:Think()
	if self.cargo then
		if  (self.cargo:IsValid()) then	
			if self.done then
				self.Entity:NextThink( CurTime() + 30 )
			else
				self.cargo.maxentititable = self.cargo.maxentititable +5
				self.done = true
			end
		else
			self.cargo = nil
		end
	else
		self.cargo = getcargo(self.Entity)
		Msg (tostring(self.cargo) .. "\n")
		if not self.cargo then
			self.Entity:NextThink( CurTime() + 5 )
			return true
		end
	end
	self.Entity:NextThink(CurTime() + 1)
	return
end

function ENT:OnTakeDamage(info)
	Shieldhit(info,self.Entity)
end